//
//  WGBAlertView.m
//  WGBAlertViewDemo
//
//  Created by 王贵彬  on 2023/5/11.
//

#import "WGBAlertView.h"

@interface WGBAlertView ()

@property (nonatomic, strong) UIView *bodyView;

@end

@implementation WGBAlertView

+ (WGBAlertView *)alertView{
    return [[WGBAlertView alloc] initWithFrame:CGRectZero];
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        self.frame = UIScreen.mainScreen.bounds;
        //初始化
        self.bodyView = [[UIView alloc] initWithFrame:UIScreen.mainScreen.bounds];
        self.bodyView.alpha = 0;
        self.bodyView.backgroundColor = [UIColor clearColor];
        [self addSubview:self.bodyView];
        self.userInteractionEnabled = YES;
        self.bodyView.userInteractionEnabled = YES;
        self.touchDismiss = NO;
        self.isUsingSpring = YES;
        self.maskColor = [[UIColor blackColor] colorWithAlphaComponent:0.4];
    }
    return self;
}


- (void)setContentView:(UIView *)contentView{
    if(self.contentView && contentView != self.contentView ){
        [self.contentView removeFromSuperview];
    }
    _contentView = contentView;
    [self.bodyView addSubview:contentView];
}

- (void)setAnimationType:(WGBAlertViewAnimationType)animationType{
    _animationType = animationType;
    
    CGFloat screenWidth = UIScreen.mainScreen.bounds.size.width;
    CGFloat screenHeight = UIScreen.mainScreen.bounds.size.height;
    CGRect frame = CGRectMake(0, 0, screenWidth, screenHeight);
    
    switch (animationType) {
        case WGBAlertViewAnimationTypeDefault:
        {
            //不需要改变
            frame.origin.y = 0;
            frame.origin.x = 0;
        }
        break;
            
        case WGBAlertViewAnimationTypeFromBottomToTop:
        {
            frame.origin.y = screenHeight;
        }
        break;
        case WGBAlertViewAnimationTypeFromTopToBottom:
        {
            frame.origin.y = -screenHeight;
        }
        break;
        case WGBAlertViewAnimationTypeFromLeftToRight:
        {
            frame.origin.x = -screenWidth;
        }
        break;
        case WGBAlertViewAnimationTypeFromRightToLeft:
        {
            frame.origin.x = screenWidth;
        }
        break;

        case WGBAlertViewAnimationTypeFromSmallToBig:
        {
            frame.origin.y = 0;
            if(self.contentView){
                self.contentView.transform = CGAffineTransformMakeScale(0.1, 0.1);
            }
        }
        break;

        case WGBAlertViewAnimationTypeFromBigToSmall:
        {
            frame.origin.y = 0;
            if(self.contentView){
                self.contentView.transform = CGAffineTransformMakeScale(1.5, 1.5);
            }
        }
        break;
        default:
            break;
    }
    self.bodyView.frame = frame;
}


/// 将弹窗添加到指定父视图上
- (void)showAtHostView:(UIView *_Nullable)hostView{
    NSTimeInterval animationDuration = 0.35;
    if(!hostView){
        hostView = [UIApplication sharedApplication].keyWindow;
    }
    [hostView addSubview:self];
    self.bodyView.alpha = 1.0;
    CGRect frame = self.bodyView.frame;
    frame.origin.y = 0;
    frame.origin.x = 0;

    if(self.isUsingSpring){
        [UIView animateWithDuration:animationDuration delay:0 usingSpringWithDamping:0.6 initialSpringVelocity:0.45 options:(UIViewAnimationOptionCurveEaseInOut) animations:^{
            self.backgroundColor = self.maskColor;
            self.bodyView.frame = frame;
            if(self.contentView){
                self.contentView.transform = CGAffineTransformIdentity;
            }
        } completion:^(BOOL finished) {
            
        }];
    }else{
        [UIView animateWithDuration:animationDuration animations:^{
            self.backgroundColor = self.maskColor;
            self.bodyView.frame = frame;
            if(self.contentView){
                self.contentView.transform = CGAffineTransformIdentity;
            }
        } completion:^(BOOL finished) {
            
        }];
    }
}


- (void)show{
    [self showAtHostView:nil];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
   [super touchesBegan:touches withEvent:event];
    if(self.touchDismiss){
        if(self.contentView){
            UITouch *touch = [touches anyObject];
            CGPoint point = [touch locationInView:self];
            CGRect bounds = self.contentView.bounds;
            CGRect rect = [self.contentView convertRect:bounds toView:self];
            if (!CGRectContainsPoint(rect,point)) {
                [self dismiss];
            }
        }else{
            [self dismiss];
        }
    }
}

- (void)dismiss{
    if(self.animationType == WGBAlertViewAnimationTypeDefault){
        self.bodyView.alpha = 0.0;
        self.backgroundColor = [UIColor clearColor];
        [self removeFromSuperview];
        !self.dismissDidEndBlock? : self.dismissDidEndBlock();
        return;
    }
    CGRect frame = self.bodyView.frame;
    frame.origin.y = UIScreen.mainScreen.bounds.size.height;
    [UIView animateWithDuration:0.35 animations:^{
        self.bodyView.alpha = 0.0;
        self.backgroundColor = [UIColor clearColor];
        self.bodyView.frame = frame;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
        !self.dismissDidEndBlock? : self.dismissDidEndBlock();
    }];
}

@end
